#include "physio_main.h"
#include "ui_physio_main.h"

#include <QLineEdit>
#include <QStyledItemDelegate>

/*
使用 QAbstractTableModel 或 QStandardItemModel 作为 QTableView 的数据模型。
在模型中重写 flags 方法，为需要编辑的单元格返回 Qt::ItemIsEditable 标志。
cpp
Copy Code
Qt::ItemFlags QAbstractTableModel::flags(const QModelIndex &index) const {
    if (!index.isValid())
        return Qt::NoItemFlags;
    return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable;
}


将委托设置到 QTableView‌：

使用 setItemDelegate 或 setItemDelegateForColumn 方法将自定义委托应用到 QTableView。
cpp
Copy Code
int main(int argc, char *argv[]) {
    QApplication app(argc, argv);

    QTableView tableView;
    MyModel model; // 假设 MyModel 继承自 QAbstractTableModel
    tableView.setModel(&model);

    MyDelegate *delegate = new MyDelegate(&tableView);
    tableView.setItemDelegate(delegate);

    tableView.show();
    return app.exec();
}
*/

class MyDelegate : public QStyledItemDelegate {
public:
    MyDelegate(QObject *parent = nullptr) : QStyledItemDelegate(parent) {}

    QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override {
        if (index.column() == 1) // 假设第2列可编辑
            return new QLineEdit(parent);
        return QStyledItemDelegate::createEditor(parent, option, index);
    }

    void setEditorData(QWidget *editor, const QModelIndex &index) const override {
        if (QLineEdit *lineEdit = qobject_cast<QLineEdit *>(editor)) {
            QString value = index.model()->data(index, Qt::EditRole).toString();
            lineEdit->setText(value);
        } else {
            QStyledItemDelegate::setEditorData(editor, index);
        }
    }

    void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override {
        if (QLineEdit *lineEdit = qobject_cast<QLineEdit *>(editor)) {
            QString value = lineEdit->text();
            model->setData(index, value, Qt::EditRole);
        } else {
            QStyledItemDelegate::setModelData(editor, model, index);
        }
    }

    void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const override {
        editor->setGeometry(option.rect);
    }
};

Physio_main::Physio_main(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Physio_main)
{
    ui->setupUi(this);
}

Physio_main::~Physio_main()
{
    delete ui;
}

void Physio_main::on_pb_exit_clicked()
{

}

#include <QCameraInfo>

void Physio_main::on_pushButton_2_clicked()
{
    QList<QCameraInfo> cameraList;
    cameraList = QCameraInfo::availableCameras();
    ui->le_invoice_no_2->setText(QString::number(cameraList.size()));
}
